package com.mlnx.work.controller;

import com.mlnx.common.annotations.NeedLogin;
import com.mlnx.common.entity.Response;
import com.mlnx.common.entity.ResponseData;
import com.mlnx.work.config.ControllerNames;
import com.mlnx.work.entity.User;
import com.mlnx.work.enums.ResponseEnum;
import com.mlnx.work.pojo.form.UserFindPassForm;
import com.mlnx.work.pojo.form.UserForm;
import com.mlnx.work.pojo.form.UserRegisterForm;
import com.mlnx.work.pojo.vo.UserVo;
import com.mlnx.work.service.UserService;

import org.apache.commons.lang3.StringUtils;
import org.shan.spring.base.BaseController;
import org.shan.spring.base.annotations.SysLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * Created by amanda.shan on 2020/5/19.
 */
@Api(value = ControllerNames.UserName + "接口", tags = {ControllerNames.UserName + "接口"})
@SysLog(title = ControllerNames.UserName)
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

    @Autowired
    private UserService userService;

    @ApiOperation(value = "注册", notes = "")
    @PostMapping("")
    public Response add(@Valid UserRegisterForm form)  {
        userService.register(form);
        return this.result();
    }

    @ApiOperation(value = "获取邮箱验证码", notes = "")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "email", value = "邮箱", required = true)
            }
    )
    @GetMapping("/email")
    public Response getPhoneCode(String email){
        if (StringUtils.isEmpty(email)){
            return result(ResponseEnum.EMAIL_INVAIL);
        }

        return result(userService.getEmailCode(email));
    }


    @NeedLogin
    @ApiOperation(value = "更改", notes = "")
    @PutMapping({"/{id}"})
    public Response modify(@PathVariable("id") Integer id, @Valid UserForm form) {
        userService.updateById(id, form);
        return this.result();
    }

    @NeedLogin
    @ApiOperation(
            value = "根据ID获取",
            notes = ""
    )
    @GetMapping({"/{id}"})
    public ResponseData<User> getById(@PathVariable("id") Integer id) {
        return this.result(userService.getById(id));
    }

    @NeedLogin
    @ApiOperation(
            value = "获取自身的用户信息",
            notes = ""
    )
    @GetMapping({"/self"})
    public ResponseData<UserVo> getSelf() {
        return this.result(userService.getSelfUser());
    }


    @ApiOperation(value = "找回密码", notes = "")
    @PutMapping({"/pass"})
    public Response modify(@Valid UserFindPassForm form) {
        userService.findPas(form);
        return this.result();
    }

}
